package com.example.xd.tuiliu_test.videoplayer.util;

import android.content.Context;
import android.view.OrientationEventListener;

import tv.danmaku.ijk.media.player.IMediaPlayer;


public class FullScreenUtils {
    //横向
    public static final int VIDEO_ORIENTATION_CROSSWISE = 1001;
    //纵向
    public static final int VIDEO_ORIENTATION_LENGTHWAYS = 1002;
    private OrientationEventListener orientationEventListener;

    public void setOnOrientationChangeListener(final OrientationListener listener, Context context) {
        orientationEventListener = new OrientationEventListener(context) {
            @Override
            public void onOrientationChanged(int orientation) {
                if (orientation > 10 && orientation < 170) {
                    listener.OrientationChange(OrientationListener.RIGHT);
                } else if (orientation > 190 && orientation < 350) {
                    listener.OrientationChange(OrientationListener.LEFT);
                }
            }
        };
        orientationEventListener.enable();
    }

    public void releaseOnOrientationChangeListener(final OrientationListener listener, Context context) {
        orientationEventListener.disable();
        orientationEventListener = null;
    }

    public static int getVideoOrientation(IMediaPlayer player) {
        int videoWidth = player.getVideoWidth();
        int videoHeight = player.getVideoHeight();
        if (videoWidth < videoHeight) {
            return VIDEO_ORIENTATION_LENGTHWAYS;
        } else {
            return VIDEO_ORIENTATION_CROSSWISE;
        }
    }

}
